<?php
/**
 * Created by PhpStorm.
 * User: 阿沁
 * Date: 2023/11/23
 * Time: 13:54
 * SOURCE: 中国大陆工时规管现况（标准工时）：
 *              一天工作时间为8小时，平均每周工时不超过40小时;
 *              加班上限为一天3小时及一个月36小时，逾时工作薪金不低于平日工资的150％。
 *              而一周最高工时则为48小时。平均每月计薪天数为21.75天。
 * TO: 我是阿沁,健康撸代码,拒绝通宵。
 * 版权所有: © 2023-2030 阿沁
 * WARNING: 未经本人许可, 禁止一切商用
 * CLASS: < CalculationThread.php >
 */

namespace app\common;

use Thread;

class CalculationThread extends Thread
{
	public $result;
	private $numbers;
	private $k;
	
	public function __construct($numbers, $k) {
		$this->numbers = $numbers;
		$this->k = $k;
	}
	
	public function run() {
		$this->result = $this->calculateCombinations($this->numbers, $this->k);
	}
	
	private function calculateCombinations($numbers, $k) {
		$result = [];
		$current = [];
		$this->backtrack($result, $current, $numbers, $k, 0);
		return $result;
	}
	
	private function backtrack(&$result, &$current, $numbers, $k, $start) {
		if (count($current) == $k) {
			$result[] = $current;
			return;
		}
		
		for ($i = $start; $i < count($numbers); $i++) {
			$current[] = $numbers[$i];
			$this->backtrack($result, $current, $numbers, $k, $i + 1);
			array_pop($current);
		}
	}
}